package com.book.book.controller;

import com.book.book.entity.JSONResponse;
import com.book.book.service.NovelService;
import com.book.chapter.common.utils.ChapterDetailSpiderFactory;
import com.book.chapter.common.utils.ChapterSpiderFactory;
import com.book.chapter.entity.Chapter;
import com.book.chapter.entity.ChapterDetail;
import com.book.chapter.service.ChapterDetailSpider;
import com.book.chapter.service.ChapterSpider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/***
 * @Author Lin
 * @Date Create By 11:31 2017/10/9
 * @Description
 */
@Controller
@RequestMapping("")
public class NovelController {

    @Autowired
    private NovelService novelService;

    @RequestMapping
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/testChapters", method = RequestMethod.GET)
    @ResponseBody
    public JSONResponse getChapter(String url) {
        ChapterSpider spider = ChapterSpiderFactory.getChapterSpider(url);
        List<Chapter> chapters = spider.getChapter(url);
        return JSONResponse.success(chapters);
    }

    @RequestMapping(value = "/testChapterDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResponse getChapterDetail(String url) {
        ChapterDetailSpider spider = ChapterDetailSpiderFactory.getChapterDetailSpider(url);
        ChapterDetail detail = spider.getChapterDetail(url);
        return JSONResponse.success(detail);
    }

    @RequestMapping("/novelSearch")
    @ResponseBody
    public JSONResponse getNovelByKeyword(String keyword) {
        return JSONResponse.success(novelService.getNovelByKeyword(keyword));
    }

    @RequestMapping("/novelSearch2")
    @ResponseBody
    public JSONResponse getNovelByKeywordAndPlatformId(String keyword, int platformId) {
        return JSONResponse.success(novelService.getNovelByKeywordAndPlatformId(keyword, platformId));
    }

    @RequestMapping(value = "/chapterList", method = RequestMethod.GET)
    public ModelAndView showChapterList(String url) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/novel/chapterList");
        view.getModel().put("chapters", ChapterSpiderFactory.getChapterSpider(url).getChapter(url));
        view.getModel().put("baseUrl", url);
        return view;
    }

    @RequestMapping(value = "/ChapterDetail", method = RequestMethod.GET)
    public ModelAndView getChapterDetail(String url, String baseUrl) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/novel/chapterDetail");
        try {
            ChapterDetail detail = ChapterDetailSpiderFactory.getChapterDetailSpider(url).getChapterDetail(url);
            detail.setContent(detail.getContent().replaceAll("\n", "<br>"));
            view.getModel().put("ChapterDetail", detail);
            view.getModel().put("isSuccess", true);
        } catch (Exception e) {
            e.printStackTrace();
            view.getModel().put("isSuccess", false);
        }
        view.getModel().put("baseUrl", baseUrl);
        return view;
    }
}
